<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="代码" prop="code"><el-input v-model="queryParams.code" placeholder="请输入代码" clearable @keyup.enter.native="handleQuery" /></el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"><el-button plain size="mini" type="primary" icon="el-icon-plus" @click="handleAdd()" v-hasPermi="['binance:order:add']">下单</el-button></el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="accountList">
      <el-table-column label="代码" align="center" prop="asset" />
      <el-table-column label="余额" align="center" prop="free" />
      <el-table-column label="锁定额" align="center" prop="locked" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-plus" @click="handleAdd(scope.row)" v-hasPermi="['binance:order:add']">下单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改交易订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="代码" prop="code"><el-input v-model="form.code" placeholder="请输入代码" /></el-form-item>
        <el-form-item label="订单方向" prop="side">
          <el-select v-model="form.side" placeholder="请选择订单方向">
            <el-option v-for="dict in dict.type.order_side" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="代币数量" prop="quantity" v-if="form.side == 'SELL'"><el-input v-model="form.quantity" placeholder="请输入下单代币数量" /></el-form-item>
        <el-form-item label="USDT数量" prop="quoteOrderQty" v-if="form.side == 'BUY'"><el-input v-model="form.quoteOrderQty" placeholder="请输入下单USDT数量" /></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { accountList } from '@/api/binance/conf';
import { addOrder } from '@/api/binance/order';

export default {
  name: 'order',
  dicts: ['order_side'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      accountList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        code: null
      },
      // 表单参数
      form: {
        code: null
      },
      // 表单校验
      rules: {
        code: [{ required: true, message: '代码不能为空', trigger: 'blur' }],
        side: [{ required: true, message: '订单方向不能为空', trigger: 'change' }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.open = true;
      this.title = '下单';
      this.form.code = row.asset;
    },
    /** 查询A股列列表 */
    getList() {
      this.loading = true;
      accountList(this.queryParams).then(response => {
        this.accountList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orgOrderId: null,
        code: null,
        side: null,
        type: null,
        price: null,
        quantity: null,
        quoteOrderQty: null,
        transactTime: null,
        executedQty: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          addOrder(this.form).then(response => {
            this.$modal.msgSuccess('下单成功');
            this.open = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>
